
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author andyisaballa
 */
public class GuessPanel extends javax.swing.JPanel {
    
    private DatabaseConnection dbConnect;
    private Game game;
    private Controller controls;
    /**
       Creates new form GuessPanel
    **/
    public GuessPanel(Controller controls, Game game, DatabaseConnection db) {
        this.dbConnect = db;
        this.game = game;
        this.controls = controls;
        initComponents();
        if (this.game.getState().equals("draw")) {
            this.guessInput.setVisible(false);
            this.guessLabel.setVisible(false);
        }
    }
    
    /**
        Sets the word for this game 
    **/
    public void setWord(String word) {
        this.game.setWord(word);
    }
    
    /**
        Sets the state for this game 
    **/
    public void setState(String state) {
        this.game.setState(state);
        if (this.game.getState().equals("guess")) {
            this.guessInput.setVisible(true);
            this.guessLabel.setVisible(true);
        }
        else {
			this.guessInput.setVisible(false);
            this.guessLabel.setVisible(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        guessInput = new javax.swing.JTextField();
        guessLabel = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(0, 51, 102));
        setPreferredSize(new java.awt.Dimension(710, 70));
        setSize(new java.awt.Dimension(710, 70));

        guessLabel.setForeground(new java.awt.Color(255, 255, 255));
        guessLabel.setText("Guess Here:");

        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(90, 90, 90)
                .add(guessLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(guessInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 80, Short.MAX_VALUE)
                .add(submitButton)
                .add(65, 65, 65))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(guessInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(guessLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 37, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(submitButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(30, 30, 30))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        if (this.game.getState().equals("draw")) {
            boolean result = dbConnect.saveImage(this.game);
            if (result) {
                this.game.clearImage();
                this.controls.openProfile();
            }
        }
        else {
            if (game.checkGuess(guessInput.getText().trim().toLowerCase())) {
                JOptionPane.showMessageDialog(
                        this.controls.frame, 
                        "You probably cheated.", "Victory", 
                        JOptionPane.PLAIN_MESSAGE
                );
            }
            else {
                JOptionPane.showMessageDialog(
                        this.controls.frame, 
                        String.format("Sorry, the word was '%s'.", game.getWord()), "Failure", 
                        JOptionPane.PLAIN_MESSAGE
                );
            }

            controls.openProfile();
        }
    }//GEN-LAST:event_submitButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField guessInput;
    private javax.swing.JLabel guessLabel;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables
}
